from pyppeteer import launch
from playwright.sync_api import Browser,sync_playwright
from lxml import etree
from tool import *
import asyncio
from shop import AdPro,Shop,PHShop
import requests
import traceback


logger = logging.getLogger()
logger.setLevel(logging.INFO)  # 设置日志级别

ms = sqlseverDB('{SQL Server}', SQL_SERVER, 'sumaitongpol', SQL_USER, SQL_PWD)
# 创建文件处理器 (Handler)
log_path=f'D:/Web/Gtupdate/GTLOG/创建广告{getTimeStr()}.log'
file_handler = logging.FileHandler(log_path, mode='a', encoding='utf-8')
#ms.ExecNoQuery('update task set logPath=? where id=13',(log_path,))

# 创建控制台处理器 (Handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)

# 设置日志格式
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)

# 将处理器添加到 logger 对象
logger.addHandler(file_handler)
logger.addHandler(console_handler)
session=requests.session()


def test_xp(task_id):

    #rs = ms.ExecQuerydict("select * from houtai where browserID in ('k16mu7vq','k16mu7vr')")
    #rs = ms.ExecQuerydict("select * from houtai where is_th_copy_right=1")
    #rs=ms.ExecQuerydict("select * from S1688Account where browserID in ('k10mtjyj')")
    rs= ms.ExecQuerydict("select * from houtai where is_kj=1 and is_cj_bigll=1")
    # rs=   [
    #         {
    #             'id': 55, 'BrowserID': 'kypn401', 'DpName': '测试店铺', 'GroupName': '测试分组', 'Platform': 'seller.shopee.co.id', 
    #             'Country': '印尼', 'PingTai': 'shopee', 'UserName': 'xiya2enztd', 'Password': 'QAwsedrf@', 'SJBM': 'cai-8897', 'YYBM': 'id8888', 
    #             'shop_status': 1, 'shop_type': '大数据铺货', 'ads_target_quantity': None, 'order_id_buyers_above': '0', 'price_times': '2.1', 
    #             'exchange_rate': 4.53,'keyword_type':1,'is_KJ':0,'shopid':'1407675909','CurrenyRate':0.41
    #         }
    #     ]

    print(f'开始,任务{task_id}')
    for r in rs:
        # r['BrowserID']='kypn401'
        if task_id in [4,47,49,50]:
            cur_shop=PHShop(r)
        else:
            cur_shop = Shop(r)
        if task_id==25:
            cur_shop.citem=(100001,'Health',0)
        if task_id in [40,46]:
            cur_shop.rocde_list=[]
        if task_id==50:
            sql_pro='''select top 2 id,url,itemid from ShopeePHADPro where is_cj=0 order by month_sold desc'''
            rs_lm=ms.ExecQuery(sql_pro)
            cur_shop.task_list=rs_lm
        res=cur_shop.start_task(task_id)
        print(f'更新数据库行数:{res}')

def test_kj_xp(task_id):
    rs = ms.ExecQuerydict("select * from houtai where browserID in ('k146j6c8')")
    print(f'开始,任务{task_id}')
    for r in rs:
        r['BrowserID']='kypn401'
        if task_id==4:
            cur_shop=PHShop(r)
        else:
            cur_shop = Shop(r)
        if task_id==25:
            cur_shop.citem=(100001,'Health',0)
        res=cur_shop.start_task(task_id)
        print(f'更新数据库行数:{res}')
    
def test_phxp():

    # rs = ms.ExecQuerydict("select * from houtai where browserID in ('kq6500p')")
    rs=ms.ExecQuerydict("select * from S1688Account where browserID in ('kx4w7ca')")




    for r in rs:
        cur_shop=PHShop(r)
        cur_shop.is_recj=True
        cur_shop.offerid_list=[('749283562168',)]
        cur_shop.start_task(36)

test_xp(1)


